/* 
 * Copyright 2014-2021 UME Framework, GNU General Public License 
 */
package com.u2framework.lib.tool.poi;

import java.math.BigDecimal;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;

/**
 * SimpleCellReader
 * 
 * @author SultansOfSwing & MaYue
 */
public class SimpleCellReader implements CellReader<Object> {

    /*
     * (non-Javadoc)
     * 
     * @see com.ibm.cn.evox.tool.common.CellReader#read(org.apache.poi.ss.usermodel .Cell)
     */
    @Override
    public Object read(Cell cell) {
        if (cell == null) {
            return null;
        }

        Object value = null;
        CellType cellType = cell.getCellType();
        if (CellType.BLANK.equals(cellType)) {
            value = null;
        } else if (CellType.BOOLEAN.equals(cellType)) {
            value = cell.getBooleanCellValue();
        } else if (CellType.NUMERIC.equals(cellType)) {
            value = BigDecimal.valueOf(cell.getNumericCellValue());
        } else if (CellType.STRING.equals(cellType)) {
            value = cell.getStringCellValue();
        } else if (CellType.FORMULA.equals(cellType)) {
            value = cell.getCellFormula();
            try {
                value = cell.getNumericCellValue();
            } catch (RuntimeException e1) {
                try {
                    value = cell.getStringCellValue();
                } catch (RuntimeException e2) {
                    value = cell.getDateCellValue();
                }
            }
        } else {
            value = cell.getStringCellValue();
        }
        return value;
    }
}
